package com.woniuxy.house.provider;

import com.woniuxy.house.entity.Source;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author baobo
 * @date 2022-08-27-16:31
 */
public class SourceSqlProvider {
	/**
	 * 用户可修改字段
	 * @param source
	 * @return
	 */
	public String update(Source source) {
		return  new SQL() {
			{
				UPDATE("house_source");
				if(source.getTitle()!=null){
					SET("title=#{title}");
				}
				if(source.getProvince()!=null){
					SET("province=#{province}");
				}
				if(source.getCity()!=null){
					SET("city=#{city}");
				}
				if(source.getArea()!=null){
					SET("area=#{area}");
				}
				if(source.getStreet()!=null){
					SET("street=#{street}");
				}
				if(source.getPrice()!=null){
					SET("price=#{price}");
				}
				if(source.getTowards()!=null){
					SET("towards=#{towards}");
				}
				if(source.getSize()!=null){
					SET("size=#{size}");
				}
				if(source.getRoomStyle()!=null){
					SET("room_style=#{roomStyle}");
				}
				if(source.getPublishTime()!=null){
					SET("publish_time=#{publishTime}");
				}
				if(source.getRentalHouseWay()!=null){
					SET("rental_house_way=#{rentalHouseWay}");
				}
				if(source.getHouseStyle()!=null){
					SET("house_style=#{houseStyle}");
				}
				if(source.getRentalTimeWay()!=null){
					SET("rental_time_way=#{rentalTimeWay}");
				}
				if(source.getTag()!=null){
					SET("tag=#{tag}");
				}
				if(source.getLongitude()!=null){
					SET("longitude=#{longitude}");
				}
				if(source.getLatitude()!=null){
					SET("latitude=#{latitude}");
				}
				if(source.getIsrental()!=null){
					SET("isrental=#{isrental}");
				}

				if(source.getIscross()!=null){
					SET("iscross=#{iscross}");
				}

				WHERE("id=#{id}");
			}
		}.toString();
	}
	public String getSourceListByCondition(Source source) {
		if(source==null){
			return  new SQL(){
				{
					SELECT("*");
					FROM("house_source");}
			}.toString();
		}else {
			return new SQL() {
				{
					SELECT("*");
					FROM("house_source");
					if (source.getTitle() != null && !"".equals(source.getTitle())) {
						WHERE("title like concat('%',#{title},'%')");
					}
					if (source.getProvince() != null) {
						WHERE("province=#{province}");
					}
					if (source.getCity() != null) {
						WHERE("city=#{city}");
					}
					if (source.getArea() != null) {
						WHERE("area=#{area}");
					}
					if (source.getStreet() != null) {
						WHERE("street=#{street}");
					}

					if (source.getStartPrice() != null) {
						WHERE("price >= #{startPrice}");
					}
					if (source.getEndPrice() != null) {
						WHERE("price <= #{endPrice}");
					}
					if (source.getTowards() != null) {
						WHERE("towards like concat('%',#{towards},'%')");
					}
					if (source.getStartSize() != null) {
						WHERE("size >= #{startSize}");
					}
					if (source.getEndSize() != null) {
						WHERE("size <=#{endSize}");
					}
					if (source.getRoomStyle() != null) {
						WHERE("room_style=#{roomStyle}");
					}
					if (source.getJuShi() != null) {
						WHERE("ju_shi=#{juShi}");
					}
					if (source.getStartJuShi() != null) {
						WHERE("ju_shi>=#{startJuShi}");
					}
					if (source.getPublishTime() != null) {
						WHERE("publish_time=#{publishTime}");
					}
					if (source.getRentalHouseWay() != null) {
						WHERE("rental_house_way=#{rentalHouseWay}");
					}
					if (source.getHouseStyle() != null && !"".equals(source.getHouseStyle())) {
						WHERE("house_style=#{houseStyle}");
					}
					if (source.getRentalTimeWay() != null) {
						WHERE("rental_time_way=#{rentalTimeWay}");
					}
					if (source.getTag() != null) {
						WHERE("tag=#{tag}");
					}
					if (source.getLongitude() != null) {
						WHERE("longitude=#{longitude}");
					}
					if (source.getLatitude() != null) {
						WHERE("latitude=#{latitude}");
					}
					if (source.getIsrental() != null && !"".equals(source.getIsrental())) {
						WHERE("isrental=#{isrental}");
					}
					if (source.getIscross() != null) {
						WHERE("iscross=#{iscross}");
					}
					if (source.getUser() != null && source.getUser().getId() != null) {
						WHERE("user_id=#{user.id}");
					}
					if (source.getOrderCondition() != null) {
						ORDER_BY("${orderCondition}");
					}
				}
			}.toString();
		}
	}
}